﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
namespace ExORMDesginer.Services
{
    /// <summary>
    /// 服务管理类
    /// </summary>
    public class ServiceManager
    {
        public static readonly ConcurrentDictionary<Type, IService> services = new ConcurrentDictionary<Type, IService>();
    
        /// <summary>
        /// 添加或获得服务方法
        /// </summary>
        /// <param name="type">服务类型</param>
        /// <param name="service">服务实例</param>
        /// <returns></returns>
        public static IService GetService(Type type,IService service)
        {
            return services.GetOrAdd(type,service);
        }
        /// <summary>
        /// 获得服务方法
        /// </summary>
        /// <typeparam name="TService">服务类型</typeparam>
        /// <returns>服务实例</returns>
        public static TService GetService<TService>() where TService:IService
        {
            if (!services.Keys.Any(t => t == typeof(TService)))
                return default(TService);
            return (TService)GetService(typeof(TService),null);
        }
    }
}
